Skip to content

Stats: fix CSV export filenames for selected date ranges - #113340

Merged
Nikschavan merged 4 commits into
trunkfrom
fix/stats-420-csv-export-filenames
Aug 7, 2026
Merged

Stats: fix CSV export filenames for selected date ranges#113340
Nikschavan merged 4 commits into
trunkfrom
fix/stats-420-csv-export-filenames

Conversation

@Nikschavan

@Nikschavan Nikschavan commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes STATS-420

Proposed Changes

  • Prefer the custom date-range query (start_date / date) when building Stats CSV download filenames, so the filename matches the selected range instead of a single period unit around the start date.
  • Omit the period segment for custom ranges (query.period is forced to day for the API and is not meaningful in the filename), e.g. …-posts-01/01/2026-08/06/2026.csv instead of …-posts-day-01/01/2026-08/06/2026.csv.
  • Extract getStatsCsvFileName and add regression coverage for the STATS-420 cases (multi-month and single-month custom ranges).
  • Drop the period and date segments from the Emails summary export filename via a new includeDates option, so it downloads as <site-slug>-emails.csv.

Why are these changes being made?

Exported report filenames did not reflect the selected date range. For a Jan 1–Aug 6 selection the file looked like a January-only month export, and for Jan 1–31 the filename used a day period with identical start/end dates. The CSV contents were correct; only the filename was wrong.

Custom date ranges also always forced query.period to day, which left a misleading day- prefix in every custom-range export even after the dates were fixed.

The Emails summary report has no date picker and its export is not date-scoped — it always returns the latest 30 emails — so a date range in that filename was misleading and is now omitted.

Testing Instructions

  1. Open Jetpack Stats → Posts (or another summary module with CSV download).
  2. Select a custom range spanning multiple months (e.g. Jan 1–Aug 6, 2026) and download CSV.
  3. Confirm the filename includes the full selected range without a period segment (e.g. …-posts-01/01/2026-08/06/2026.csv), not …-posts-day-… or a single month.
  4. Select a single-month custom range (e.g. Jan 1–31) and download CSV.
  5. Confirm the filename uses that full range without a period segment, not …-day-01/01/2026-01/01/2026.csv.
  6. Select a standard period without a custom range (e.g. month view) and confirm the filename still uses that period’s label and start/end (e.g. …-month-…).
  7. Open Stats → Emails (/stats/day/emails/<site>) and download CSV. Confirm the filename is <site-slug>-emails.csv with no period or dates.
  8. Optional: yarn test-client client/my-sites/stats/stats-download-csv/test/get-stats-csv-filename.js

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • For UI changes, have you tested the affected components in dark mode?
  • Have you tested accessibility for your changes? Ensure the feature remains usable with various user agents (e.g., browsers), interfaces (e.g., keyboard navigation), and assistive technologies (e.g., screen readers) (PCYsg-S3g-p2).
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
    • For UI changes, have we tested the change in various languages (for example, ES, PT, FR, or DE)? The length of text and words vary significantly between languages.
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

Made with Cursor

@Nikschavan
Nikschavan marked this pull request as ready for review August 7, 2026 03:11
Copilot AI review requested due to automatic review settings August 7, 2026 03:11
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 7, 2026
Nikschavan and others added 2 commits August 7, 2026 08:42
Use the custom range query dates (and period) for download filenames so exports match the selected selection instead of a single period unit.

Co-authored-by: Cursor <cursoragent@cursor.com>
The Emails summary export returns the latest 30 emails with no date scoping, so a period date range in the filename was misleading. Add an includeDates option to getStatsCsvFileName and turn it off for the Emails summary download.
@Nikschavan
Nikschavan force-pushed the fix/stats-420-csv-export-filenames branch from 056b671 to 39af32b Compare August 7, 2026 03:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Stats CSV export filenames so they reflect the user’s selected date range (especially for custom multi-month/single-month ranges), and updates the Emails summary CSV export to omit date/period segments because that export isn’t date-scoped.

Changes:

  • Extracted a shared getStatsCsvFileName helper and updated StatsDownloadCsv to use it (including an includeDates option).
  • Preferred custom range query params (start_date / date / period) when present to build filenames that match the actual selection.
  • Added regression tests covering the STATS-420 custom-range cases and the Emails includeDates=false behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
client/my-sites/stats/stats-email-summary/index.jsx Passes includeDates={ false } for the Emails summary CSV export to omit misleading date/period segments.
client/my-sites/stats/stats-download-csv/index.jsx Switches filename construction to the extracted helper and threads through includeDates.
client/my-sites/stats/stats-download-csv/get-stats-csv-filename.js Adds the helper that builds CSV filenames from either custom query ranges or legacy period bounds.
client/my-sites/stats/stats-download-csv/test/get-stats-csv-filename.js Adds regression coverage for custom-range filename generation and the includeDates=false behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread client/my-sites/stats/stats-download-csv/get-stats-csv-filename.js Outdated
Custom date ranges force query.period to day for the API, which produced
misleading day- prefixes like posts-day-01/01/2026-08/06/2026.csv. Drop the
period segment when start_date/date are present; keep it for standard
period views.
@Nikschavan Nikschavan self-assigned this Aug 7, 2026
Strict-parse custom start_date/date values and fall back to period bounds
when either is invalid, and format custom dates with the period moment
locale. Restore the previous moment locale after tests to avoid leaks.
@Nikschavan
Nikschavan requested review from a team and chihsuan August 7, 2026 04:09

@kangzj kangzj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the filename logic, the callers, and the tests — looks good.

  • Custom-range detection (strict-parsed start_date + date) matches how summary/index.jsx builds moduleQuery, and omitting the period segment is correct since query.period is forced to day for custom ranges.
  • Checked all four DownloadCsv call sites: utm-export-button passes no query so behavior is unchanged, and includeDates={false} for the all-time Emails summary is consistent with why that surface ignores the period.
  • Earlier Copilot feedback (strict parsing, test locale leak) is already addressed in a87c262.
  • New unit tests pass locally.

@Nikschavan
Nikschavan merged commit 1295bb8 into trunk Aug 7, 2026
22 checks passed
@Nikschavan
Nikschavan deleted the fix/stats-420-csv-export-filenames branch August 7, 2026 04:32
@github-actions github-actions Bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants